package com.wfg.blog.contorller;

import com.wfg.blog.common.aop.LogAnnotation;
import com.wfg.blog.common.cache.Cache;
import com.wfg.blog.service.ArticleService;
import com.wfg.blog.vo.ArticleVo;
import com.wfg.blog.vo.Result;
import com.wfg.blog.vo.params.ArticleParam;
import com.wfg.blog.vo.params.PageParams;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

//json数据进行交互
@RestController
@RequestMapping("articles")
public class ArticleController {

    @Autowired
    private ArticleService articleService;


    /**
     * 搜索功能
     * @param articleParam
     * @return
     */
    @PostMapping("search")
    //加上注解 对此接口记录日志
    public Result search(@RequestBody ArticleParam articleParam){
        //ArticleVo 页面接收的数据
        String search = articleParam.getSearch();
        return articleService.searchArticle(search);
    }
    /**
     * 首页文章列表
     * @param pageParams
     * @return
     */
    @PostMapping
    //加上注解 对此接口记录日志
    @LogAnnotation(module = "文章" ,operator = "获取文章列表")
    //缓存5分钟
    @Cache(expire = 5 * 60 * 1000,name = "listArticle")
    public Result listArticle(@RequestBody PageParams pageParams){
        //ArticleVo 页面接收的数据
        return articleService.listArticle(pageParams);
    }

    /**
     * 首页最热文章
     * @return
     */
    @PostMapping("hot")
    @Cache(expire = 5 * 60 * 1000,name = "hotArticle")
    public Result hotArticle(){
        //定义取前5条
        int limit = 5;
        return articleService.hostArticle(limit);
    }

    /**
     * 最新文章
     * @return
     */
    @PostMapping("new")
//    @Cache(expire = 5 * 60 * 1000,name = "newArticles")
    public Result newArticles(){
        //定义取前5条
        int limit = 5;
        return articleService.newArticle(limit);
    }
    /**
     * 最新文章 归档
     * @return
     */
    @PostMapping("listArchives")
//    @Cache(expire = 5 * 60 * 1000,name = "listArchives")
    public Result listArchives(){
        //定义取前5条
        return articleService.listArchives();
    }

    /**
     * 查看文章详情
     * @param articleId
     * @return
     */
    @PostMapping("view/{id}")
    public Result findArticleById(@PathVariable("id") Long articleId){

        return  articleService.findArticleById(articleId);
    }

    /**
     * 发布文章
     * @param articleParam
     * @return
     */
    // /articles/publish
    @PostMapping("publish")
    public Result publish(@RequestBody ArticleParam articleParam){

        return articleService.publish(articleParam);
    }
}
